package servlet;

import modle.Goods;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.GoodsService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@WebServlet(name = "AdminGoodsAddServlet", urlPatterns = "/admin/goods_add")
public class AdminGoodsAddServlet extends HttpServlet {
    private GoodsService gService = new GoodsService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        DiskFileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            List<FileItem> list = upload.parseRequest(request);

            Goods goods = new Goods();

            for (FileItem item :
                    list) {

                if (item.isFormField()){
                    switch (item.getFieldName()) {
                        case "name":
                            goods.setName(item.getString("utf-8"));break;
                        case "price":
                            goods.setPrice(Float.parseFloat(item.getString("utf-8")));break;
                        case "intro":
                            goods.setIntro(item.getString("utf-8"));break;
                        case "stock":
                            goods.setStock(Integer.parseInt(item.getString("utf-8")));break;
                        case "typeid":
                            goods.setTypeid(Integer.parseInt(item.getString("utf-8")));break;
                    }
                }else {
                    String fileName = item.getName();

                    fileName = "/"+new Date().getTime()+fileName;

                    String path = this.getServletContext().getRealPath("/picture")+fileName;

                    InputStream in = item.getInputStream();
                    FileOutputStream out = new FileOutputStream(path);

                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len=in.read(buffer))>0) {
                        out.write(buffer);
                    }

                    in.close();
                    out.close();
                    item.delete();


                    switch (item.getFieldName()) {
                        case "cover":
                            goods.setCover("/picture"+fileName);break;
                        case "image1":
                            goods.setImage1("/picture"+fileName);break;
                        case "image2":
                            goods.setImage2("/picture"+fileName);break;
                    }

                    }

            }


            gService.insert(goods);

            request.getRequestDispatcher("/admin/goods_list").forward(request,response);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }
}
